package handler

import (
	"context"
	"device-modbus/internal/biz/dao"
	"device-modbus/internal/logic"
	"device-modbus/pkg/r"
	"github.com/cloudwego/hertz/pkg/app"
)

type DeviceForwardRuleApi struct {
}

func (h DeviceForwardRuleApi) Add(ctx context.Context, c *app.RequestContext) {
	req := &dao.DeviceForwardRuleAddReq{}
	err := c.BindAndValidate(req)
	if err != nil {
		r.ErrorMsg(c, err.Error())
		return
	}
	logic.NewDeviceForwardRuleLogic(ctx).Add(req)
	r.OK(c)
}

func (h DeviceForwardRuleApi) Update(ctx context.Context, c *app.RequestContext) {
	req := &dao.DeviceForwardRuleUpdateReq{}
	err := c.BindAndValidate(req)
	if err != nil {
		r.ErrorMsg(c, err.Error())
		return
	}
	err = logic.NewDeviceForwardRuleLogic(ctx).Update(req)
	r.OkOrError(c, err)
}
func (h DeviceForwardRuleApi) Del(ctx context.Context, c *app.RequestContext) {
	err := logic.NewDeviceForwardRuleLogic(ctx).Del(c.Param("id"))
	r.OkOrError(c, err)
}
func (h DeviceForwardRuleApi) FindByDeviceId(ctx context.Context, c *app.RequestContext) {
	rules, err := logic.NewDeviceForwardRuleLogic(ctx).FindByDeviceId(c.Param("deviceId"))
	r.OkOrErrorData(c, rules, err)
}
